<?php
namespace admin\Controller;
use Think\Controller;
class CategoryController extends CommonController {

public function _initialize()
{
    parent::_initialize();
    $data1['value']=$this->get_cat_level(1);
	$data2['value']=$this->get_cat_level(2);
	M('Config')->where("`name`='UU_CAT_LEVEL_QUESTION'")->save($data1);
	M('Config')->where("`name`='UU_CAT_LEVEL_ARTICLE'")->save($data2);
	if(!uu_refresh_cache('config')){
	$this->error('生成配置缓存文件失败！');
	}
} 
//问题分类
	public function cat_question(){
		
		$data = M("Category");
		$c_type=I('get.c_type',1,'intval');
		if(IS_AJAX)
		{
			$map['c_type']=$c_type;
			$map['c_pid']=I('get.c_pid',0,'intval');
			$list = $data->where($map)->order('c_sort desc, c_id asc')->select();
			if(!empty($list)){
				foreach($list as $k=>$v){
					$list[$k]['editurl']="/admin.php/category/cat_question_edit/c_id/".$v['c_id'];
					$list[$k]['deleteurl']="/admin.php/category/delete/c_id/".$v['c_id'];
				}
			}
			$ajax['status']  = 1;
			$ajax['content'] = $list;
		 	$this->ajaxReturn($ajax);
		}
		else
		{
			$map['c_type']=$c_type;
			$map['c_pid']=0;
			$list = $data->where($map)->order('c_sort desc, c_id asc')->select();
			$this->assign('c_type',$map['c_type']);
			$this->assign('list',$list);
			$this->display();
		}
	}
//添加问题分类
	public function cat_question_add(){
		$data = D("Category");
		$c_type=I('request.c_type');
		if(IS_POST)
		{
				if (!$data->create()){
					$this->error($data->getError());
				}
				else
				{
					$data->c_sort = intval($data->c_sort);
					$data->add();
					$this->writeJS();
					$this->success('保存完成',I('post.url'));			
				}
		}
		else
		{
			$map['c_type']=$c_type;
			$map['c_pid']=0;
			$catlist = $data->where($map)->order('c_sort desc, c_id asc')->select();
			$this->assign('catlist',$catlist);
			$this->assign('c_type',$c_type);
			$c_id=I('request.c_id','','intval');
			if (!empty($c_id))
			{
				$c_id_info = $data->where("c_id = ".$c_id)->limit(1)->find();
				$this->assign('c_pid_cn',$c_id_info['c_title']);
				$this->assign('c_pid',$c_id);
			}
		 	$this->display();
		}
	}
//修改问题分类
	public function cat_question_edit(){
		if(IS_POST)
		{
				$data = D("Category");
				if (!$data->create()){
					$this->error($data->getError());
				}
				else
				{
					$data->save();
					$this->writeJS();
					$this->success('保存完成',I('post.url'));
				}
		}
		else
		{
			$id=I('get.c_id',0,'intval');
			if ($id)
			{
				$data = M("Category");
				$map['c_id']=$id;
				$show=$data->where($map)->limit(1)->find();
				$cat_info = $data->where("c_id = ".$show['c_pid'])->limit(1)->find();
				$this->assign('c_pid_cn',$cat_info['c_title']);
				$this->assign('c_type',$show['c_type']);
				$this->assign('show',$show);
				$catmap['c_type']=$show['c_type'];
				$catmap['c_pid']=0;
				$catlist = $data->where($catmap)->order('c_sort desc, c_id asc')->select();
				$this->assign('catlist',$catlist);
				$this->display();
				//dump($show);
			}
			else
			{
			$this->error('非法请求');
			}
		}
	}
//文章分类
	public function cat_article(){
		$data = M("Category");
		$c_type=I('get.c_type',2,'intval');
		if(IS_AJAX)
		{
			$map['c_type']=$c_type;
			$map['c_pid']=I('get.c_pid',0,'intval');
			$list = $data->where($map)->order('c_sort desc, c_id asc')->select();
			if(!empty($list)){
				 foreach($list as  $key => $value)
				 {
//					 $list[$key]['addurl']=RU('Category/cat_tag_add',array('c_id'=>$list[$key]['c_id'],'c_type'=>$list[$key]['c_type']));
					 $list[$key]['editurl']=RU('Category/cat_article_edit',array('c_id'=>$list[$key]['c_id']));
					 $list[$key]['deleteurl']=RU('Category/delete',array('c_id'=>$list[$key]['c_id']));
				 }
			}
			$ajax['status']  = 1;
			$ajax['content'] = $list;
		 	$this->ajaxReturn($ajax);
		}
		else
		{
			$map['c_type']=$c_type;
			$map['c_pid']=0;
			$list = $data->where($map)->order('c_sort desc, c_id asc')->select();
			$this->assign('c_type',$map['c_type']);
			$this->assign('list',$list);
			$this->display();
		}
	}
//添加文章分类
	public function cat_article_add(){
		$data = D("Category");
		$c_type=I('request.c_type');
		if(IS_POST)
		{
				if (!$data->create()){
					$this->error($data->getError());
				}
				else
				{
					$data->c_sort = intval($data->c_sort);
					$data->add();
					$this->success('保存完成',I('post.url'));				
				}
		}
		else
		{
			$map['c_type']=$c_type;
			$map['c_pid']=0;
			$catlist = $data->where($map)->order('c_sort desc, c_id asc')->select();
			$this->assign('catlist',$catlist);
			$this->assign('c_type',$c_type);
			$c_id=I('request.c_id','','intval');
			if (!empty($c_id))
			{
				$c_id_info = $data->where("c_id = ".$c_id)->limit(1)->find();
				$this->assign('c_pid_cn',$c_id_info['c_title']);
				$this->assign('c_pid',$c_id);
			}
		 	$this->display();
		}
	}
//修改文章分类
	public function cat_article_edit(){
		if(IS_POST)
		{
				$data = D("Category");
				if (!$data->create()){
					$this->error($data->getError());
				}
				else
				{
					$data->save();
					$this->success('保存完成',I('post.url'));
				}
		}
		else
		{
			$id=I('get.c_id',0,'intval');
			if ($id)
			{
				$data = M("Category");
				$map['c_id']=$id;
				$show=$data->where($map)->limit(1)->find();
				$cat_info = $data->where("c_id = ".$show['c_pid'])->limit(1)->find();
				$this->assign('c_pid_cn',$cat_info['c_title']);
				$this->assign('c_type',$show['c_type']);
				$this->assign('show',$show);
				$catmap['c_type']=$show['c_type'];
				$catmap['c_pid']=0;
				$catlist = $data->where($catmap)->order('c_sort desc, c_id asc')->select();
				$this->assign('catlist',$catlist);
				$this->display();
				//dump($show);
			}
			else
			{
			$this->error('非法请求');
			}
		}
	}
//删除任意分类
	public function delete(){
	
		$ids = I('request.c_id');
		if (!is_array($ids))
		{
			$ids=array($ids);
		}
		$ids=implode(",",$ids);
		if ($ids && preg_match("/^(\d{1,10},)*(\d{1,10})$/",$ids))
		{
			$data = M("Category");
			$s=$data->delete($ids);
			$this->writeJS();
			$this->success("成功删除{$s}条数据");
		}
		else
		{
			$this->error('请选择要删除的信息！');
		}
	
 	}
//保存所有分类
	public function allSave(){
	 	$c_id = I('post.c_id');
        $c_title = I('post.c_title');
        $c_sort = I('post.c_sort');
		$add_type = I('post.add_type');
 		$add_pid = I('post.add_pid');
        $add_title = I('post.add_title');
        $add_sort = I('post.add_sort');
		$num = 0;
		$data = M("Category");
		if (is_array($c_id) && count($c_id)>0)
        {
            foreach($c_id as $k=>$v)
            {
                unset($map);
                $map['c_id'] = intval($v);
                $map['c_title']=trim($c_title[$k]);
                $map['c_sort']=intval($c_sort[$k]);
				$data->save($map);
                $num++;
         
            }
        }
		if (is_array($add_pid) && count($add_pid)>0)
        {
            foreach($add_pid as $k=>$v)
            {
                unset($map);
				$map['c_pid']  =intval($v);
                $map['c_type'] =intval($add_type[$k]);
                $map['c_title']=trim($add_title[$k]);
				$map['c_sort'] =trim($add_sort[$k]);
				if (!empty($map['c_title']))
				{
				$data->add($map);
                $num++;
		 		}
            }
        }
		if($num>0){
			$this->writeJS();
            $this->success('成功保存'.$num.'个分类！');
        }else{
            $this->error('未保存任何分类！');
        }
		 
	
 	}
	
 
}
?>